package com.lx.util.secure.algorithm.coder;


import com.lx.annotation.Note;
import com.lx.util.secure.Algorithm;

import java.nio.charset.StandardCharsets;

public class Base64 implements Coder{
    private Base64(){}
    private static final Base64 BASE_64 = new Base64();

    public static Base64 getCoder() {
        return BASE_64;
    }


    @Override
    public byte[] encode(byte[] bytes) {
        return java.util.Base64.getEncoder().encode(bytes);
    }

    @Override
    public byte[] encode(String data) {
        return encode(data.getBytes(StandardCharsets.UTF_8));
    }

    public String encodeToString(byte[] bytes) {
        return new String(encode(bytes),StandardCharsets.UTF_8);
    }

    @Override
    public String encodeToString(String data) {
        return encodeToString(data.getBytes(StandardCharsets.UTF_8));
    }

    @Override
    public byte[] decode(byte[] data) {
        return java.util.Base64.getDecoder().decode(data);
    }

    @Override
    public byte[] decode(String data) {
        data = data.replaceAll("\\s","");
        return decode(data.getBytes(StandardCharsets.UTF_8));
    }

    @Override
    public String decodeToString(byte[] bytes) {
        return new String(decode(bytes),StandardCharsets.UTF_8);
    }

    @Override
    public String decodeToString(String data) {
        return decodeToString(data.getBytes(StandardCharsets.UTF_8));
    }

}
